#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
LL rd(){
    LL x=0,f=1;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = (x<<3)+(x<<1)+(c^48);
        c = getchar();
    }
    return x*f;
}
const int N = 15;
int b[N],a[N],c[N],x[5];
string s[N];
LL n,m,K;
int q[N],sum;
int check(int n0){
    vector<int>rec[2];
    for(int i=1;i<=n;i++) rec[0].pb(i);
    for(int r=1;(1ll<<r)<=n0;r++){
        int t = n0/(1ll<<r);
        int o=r&1,p=o^1;
        rec[o].clear();
        for(int i=0;i<t/2;i++){
            if(s[r][i]=='0') {
                if(q[rec[p][i*2]]>=r) rec[o].pb(rec[p][i*2]);
                else rec[o].pb(rec[p][i*2+1]);
            }else{
                if(q[rec[p][i*2]]>=r) rec[o].pb(rec[p][i*2]);
                else rec[o].pb(rec[p][i*2+1]);
            }
        }
        if((1ll<<r)==n0) return rec[o][0];
    }
}
int vis[N];
void dfs(int n0,int now,int lim){
    if(now==lim+1){
        vis[check(lim)]=1;
        return ;
    }
    for(int i=0;i<=8;i++) {
        q[now] = i;
        dfs(n0,now+1,lim);
    }
}
LL qry(int n0){
    memset(q,0,sizeof q);
    int k1=0;
    for(k1;k1<n0;k1<<=1);
    for(int i=1;i<=n0;i++) q[i] = a[i];
    sum = 0;
    dfs(n0,n0+1,k1);
    for(int i=1;i<=k1;i++) if(vis[i]) sum+=i;
    return sum;
}
void solve(){
    for(int i=0;i<=3;i++) x[i]=rd();
    for(int i=1;i<=n;i++){
        a[i] = b[i]^x[i%4];
    }
    LL res = 0;
    for(int i=1;i<=m;i++){
        res ^= (1ll*i*qry(c[i]));
    }
    cout<<res<<'\n';
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=rd(),m=rd();
    for(int i=1;i<=n;i++) b[i]=rd();
    for(int i=1;i<=m;i++) c[i]=rd();
    K=0;
    for(K;(1<<K)<n;K++);
    for(int i=1;i<=K;i++){
        cin>>s[i];
    }
    int T =rd();
    while(T--) solve();
    return 0;
}

